﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Windows.Forms;
using System.Configuration;

namespace  FileOperationWinform
{
    public class FileOpera
    {
        private static readonly string exceptlocation = ConfigurationManager.AppSettings["Except"];
        private string strlocaion = Path.Combine(Application.StartupPath, exceptlocation);

        private static readonly string SkinFile = ConfigurationManager.AppSettings["SkinFile"];
        private string SkinFilel = Path.Combine(Application.StartupPath, SkinFile);

        public int FileAdd(int index, string num, string value)
        {
            if (!File.Exists(strlocaion))
            {
                FileStream fs1 = new FileStream(strlocaion, FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs1);
                sw.WriteLine("index=" + index + "," + num + "," + value);
                sw.Close();
                fs1.Close();
            }
            else
            {
                StreamWriter sw1 = File.AppendText(strlocaion);
                sw1.WriteLine("index=" + index + "," + num + "," + value);
                sw1.Close();
            }

            return 1;
        }
        public int FileDel(int index)
        {
            FileStream fs = new FileStream(strlocaion, FileMode.Open, FileAccess.Read);
            StreamReader sw = new StreamReader(fs);

            string line = sw.ReadLine();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; line != null; i++)
            {
                if (i != index)
                {
                    sb.Append(line + "\r\n");
                }
                line = sw.ReadLine();
            }
            sw.Close();
            fs.Close();
            FileStream fs1 = new FileStream(strlocaion, FileMode.Create, FileAccess.Write);
            StreamWriter sw1 = new StreamWriter(fs1);
            sw1.Write(sb.ToString());
            sw1.Close();
            fs1.Close();
            return 1;
        }
        public int updateFile(int index, string num, string value)
        {
            FileStream fs = new FileStream(strlocaion, FileMode.Open, FileAccess.Read);
            StreamReader sw = new StreamReader(fs);

            string line = sw.ReadLine();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; line != null; i++)
            {
                if (i == index)
                {
                    sb.Append("index=" + index + "," + num + "," + value + "\r\n");
                }
                else
                    sb.Append(line + "\r\n");
                line = sw.ReadLine();
            }
            sw.Close();
            fs.Close();
            FileStream fs1 = new FileStream(strlocaion, FileMode.Create, FileAccess.Write);
            StreamWriter sw1 = new StreamWriter(fs1);
            sw1.Write(sb.ToString());
            sw1.Close();
            fs1.Close();
            return 1;
        }
        public int SerorceFile(string value)
        {
            if (File.Exists(strlocaion))
            {
                using(FileStream fs= new FileStream(strlocaion, FileMode.Open, FileAccess.Read)){
                StreamReader sw= new StreamReader(fs);
                string[] strtemp = new string[3];
                string[] str = new string[2];
                string line = sw.ReadLine();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; line != null; i++)
                {
                    strtemp = line.Split(',');
                    str[1] = strtemp[2];
                    if ((string.Compare(str[1], value) == 0))
                    {
                        fs.Close();
                        sw.Close();
                        return 1;
                    }
                    line=sw.ReadLine();
                }
                fs.Close();
                sw.Close();
                }
            }
            return 0;
        }
        private int ReadSkinFile()
        {
            if (File.Exists(SkinFilel))
            {
                using(FileStream fs = new FileStream(SkinFilel, FileMode.Open, FileAccess.Read)){
                StreamReader sw = new StreamReader(fs);
                string line = sw.ReadLine();
                string[] str = new string[2];
                string[] temp = new string[2];
                str = line.Split('=');
                temp[1] = str[1];
                fs.Dispose();
                sw.Dispose();
                return Convert.ToInt32(temp[1]);
            }
            }
            else
            {
                return 0;
            }
        }
        public string SkinLoad()
        {
            int x = ReadSkinFile();
            string str = String.Empty;
            try
            {
                switch (x)
                {
                    case 0:
                        {
                            str = Path.Combine(Application.StartupPath, @"Skin\MSN.ssk");
                            break;
                        }
                    case 1:
                        {
                            str = Path.Combine(Application.StartupPath, @"Skin\Eighteen.ssk");
                            break;
                        }
                    case 2:
                        {
                            str = Path.Combine(Application.StartupPath, @"Skin\vista1.ssk");
                            break;
                        }
                    case 3:
                        {
                            str = Path.Combine(Application.StartupPath, @"Skin\SportsBlack.ssk");
                            break;
                        }
                    case 4:
                        {
                            str = Path.Combine(Application.StartupPath, @"Skin\Emerald.ssk");
                            break;
                        }
                }
                return str;
            }
            catch
            {
                str = "Error";
                return str;
            }
        }
    }
}
